<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
JsWs Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
	
		parent.document.title="connectionhandler.js Overview";
	
}
</script>
</head>
<body bgcolor="white" onload="asd();">

<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> 	<font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top">
<em>
<b>JsWs</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<center>
	
	   <h2>connectionhandler.js</h2>
	
</center>

	


<h4>Summary</h4>
<p>
	
		No overview generated for 'connectionhandler.js'<BR/><BR/>
	
</p>

<hr>


    <table border="1" cellpadding="3" cellspacing="0" width="100%">
    <tr bgcolor="#CCCCFF" class="TableHeadingColor">
    <td colspan=2><font size="+2">
    
        <b>Class Summary</b>
    
    </font></td>
    </tr>
    
    <tr bgcolor="white" class="TableRowColor">
    <td width="15%"><b><a href="JsWsConnectionHandler.html">JsWsConnectionHandler</a></b></td>
    <td>Handles an incoming socket connection
 </td>
    </tr>
    
    </table>
    <hr/> 


<!-- ========== METHOD SUMMARY =========== -->

<!-- ========== END METHOD SUMMARY =========== -->


        <pre class="sourceview"><span class="comment">/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is JsWs.
 *
 * The Initial Developer of the Original Code is
 * Chris Winberry.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */</span>


<span class="comment">/********************************************************************************
 * JsWsConnectionHandler
 *******************************************************************************/</span>

<span class="comment">/**
 * <span class="attrib">@class</span> Handles an incoming socket connection
 * <span class="attrib">@constructor</span>
 * <span class="attrib">@param</span> {JsWs} server JsWs server socket listener
 * <span class="attrib">@param</span> {nsIServerSocket} socket Incoming accepted socket
 * <span class="attrib">@param</span> {nsISocketTransport} transport Socket transport
 */</span>
<span class="reserved">function</span> JsWsConnectionHandler (server, socket, transport) {
	<span class="comment">/**	
	 * JsWs server socket listener
	 * <span class="attrib">@type</span> JsWs
	 */</span>
	<span class="reserved">this</span>.server = server;
	<span class="comment">/**	
	 * Incoming accepted socket
	 * <span class="attrib">@type</span> nsIServerSocket
	 */</span>
	<span class="reserved">this</span>.socket = socket;
	<span class="comment">/**	
	 * Socket transport
	 * <span class="attrib">@type</span> nsISocketTransport
	 */</span>
	<span class="reserved">this</span>.transport = transport;
	<span class="comment">/**	
	 * Request object
	 * <span class="attrib">@type</span> Request
	 */</span>
	<span class="reserved">this</span>.request = new Request();
	<span class="comment">/**	
	 * Response object
	 * <span class="attrib">@type</span> Response
	 */</span>
	<span class="reserved">this</span>.response = new Response(<span class="reserved">this</span>);

	<span class="reserved">if</span> (transport) {
		<span class="comment">/** <span class="attrib">@private</span> */</span>
		<span class="reserved">this</span>.outstream = <span class="reserved">this</span>.transport.openOutputStream(1, 0, 0);
		<span class="comment">/** <span class="attrib">@private</span> */</span>
		<span class="reserved">this</span>.stream = <span class="reserved">this</span>.transport.openInputStream(0, 0, 0);
		<span class="comment">/** <span class="attrib">@private</span> */</span>
		<span class="reserved">this</span>.instream = Cc[<span class="literal">"@mozilla.org/binaryinputstream;1"</span>].
			createInstance(Ci.nsIBinaryInputStream);
		<span class="reserved">this</span>.instream.setInputStream(<span class="reserved">this</span>.stream);
		<span class="comment">/** <span class="attrib">@private</span> */</span>
		<span class="reserved">this</span>.dataPump = Components.
			classes[<span class="literal">"@mozilla.org/network/input-stream-pump;1"</span>].
			createInstance(Ci.nsIInputStreamPump);
		<span class="reserved">this</span>.dataPump.init(<span class="reserved">this</span>.stream, -1, -1, 0, 0, false);
		<span class="reserved">this</span>.dataPump.asyncRead(<span class="reserved">this</span>, null);
	}
}


<span class="comment">/**
 * Runs cleanup on internal objects
 */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.destroy = <span class="reserved">function</span> () {
	<span class="reserved">this</span>.server = null;
	<span class="reserved">this</span>.socket = null;
	<span class="reserved">this</span>.transport = null;
	<span class="reserved">if</span> (<span class="reserved">this</span>.outstream)
		<span class="reserved">this</span>.outstream.close();
	<span class="reserved">this</span>.outstream = null;
	<span class="reserved">if</span> (<span class="reserved">this</span>.request)
		<span class="reserved">this</span>.request.destroy();
	<span class="reserved">this</span>.request = null;
	<span class="reserved">if</span> (<span class="reserved">this</span>.response)
		<span class="reserved">this</span>.response.destroy();
	<span class="reserved">this</span>.response = null;
	<span class="reserved">if</span> (<span class="reserved">this</span>.instream)
		<span class="reserved">this</span>.instream.close();
	<span class="reserved">this</span>.instream = null;
	<span class="reserved">if</span> (<span class="reserved">this</span>.stream)
		<span class="reserved">this</span>.stream.close();
	<span class="reserved">this</span>.stream = null;
	<span class="reserved">this</span>.dataPump = null;
}


<span class="comment">/** <span class="attrib">@private</span> */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.onStartRequest = <span class="reserved">function</span> (request, context) { }

<span class="comment">/** <span class="attrib">@private</span> */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.onStopRequest = <span class="reserved">function</span> (request, context, status) {
	<span class="reserved">this</span>.destroy();
}


<span class="comment">/**
 * Writes the data to the connected socket's output stream
 * <span class="attrib">@param</span> {String} data Data to write to the stream
 */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.write = <span class="reserved">function</span> (data) {
	var realSize = data.length;
	var writeSize = <span class="reserved">this</span>.outstream.write(data, data.length);
}


<span class="comment">/** <span class="attrib">@private</span> */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.handleRequest = <span class="reserved">function</span> () {
	var script = new ScriptContext(<span class="reserved">this</span>.server, <span class="reserved">this</span>.request.path);
	var codeHost = new EvalCodeHost(<span class="reserved">this</span>.server, <span class="reserved">this</span>.request, <span class="reserved">this</span>.response, script);

	try {
		try {
			var mappedModule = <span class="reserved">this</span>.server.mapModule(script.path.path);
			<span class="reserved">if</span> (!mappedModule.handler)
				mappedModule = <span class="reserved">this</span>.server.modules[JSWS_MODULES_ROOTKEY];
			codeHost.require(
				mappedModule.virtualPath +
				mappedModule.handler
				);
		} catch (ex) {
			<span class="reserved">if</span> (!(ex instanceof ExResponseEnd)) {
				var _ex = (ex instanceof ExBase) ? ex : new ExBase(<span class="literal">"Uncaught top-level"</span>, 0, ex);
				<span class="reserved">this</span>.response.status = 500;
				<span class="reserved">this</span>.response.clear();
				<span class="reserved">this</span>.response.contentType(<span class="literal">"text/html"</span>);
				<span class="reserved">this</span>.response.write(<span class="literal">"&lt;hr&gt;&lt;pre&gt;Server Error: "</span> + _ex + <span class="literal">"&lt;/pre&gt;&lt;hr&gt;"</span>);
			}
		}
		<span class="reserved">this</span>.response.end();
	} catch (e) { }

	codeHost.destroy();
	codeHost = null;
	script.destroy();
	script = null;
}


<span class="comment">/** <span class="attrib">@private</span> */</span>
JsWsConnectionHandler.<span class="reserved">prototype</span>.onDataAvailable = <span class="reserved">function</span> (request, context, inputStream, offset, count) {
	try {
		<span class="comment">// TODO: Handle chunked input from body (uploads)</span>
		<span class="reserved">if</span> (<span class="reserved">this</span>.request.parse(<span class="reserved">this</span>.instream.readBytes(<span class="reserved">this</span>.instream.available()))) {
			<span class="reserved">this</span>.handleRequest();
			<span class="reserved">this</span>.instream.close();
		}
	} catch (e) { JsWs.log(e); }
}

</pre>
	<hr>



<!-- ========== START OF NAVBAR ========== -->
<a name="navbar_top"><!-- --></a>
<table border="0" width="100%" cellpadding="1" cellspacing="0">
<tr>
<td colspan=2 bgcolor="#EEEEFF" class="NavBarCell1">
<a name="navbar_top_firstrow"><!-- --></a>
<table border="0" cellpadding="0" cellspacing="3">
  <tr align="center" valign="top">
  
  
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-summary.html"><font class="NavBarFont1"><b>Overview</b></font></a>&nbsp;</td>
  <td bgcolor="#FFFFFF" class="NavBarCell1Rev">	&nbsp;<font class="NavBarFont1Rev"><b>File</b></font>&nbsp;</td>
  

  <td bgcolor="#FFFFFF" class="NavBarCell1"> <font class="NavBarFont1">Class</font>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="overview-tree.html"><font class="NavBarFont1"><b>Tree</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="index-all.html"--><font class="NavBarFont1"><b>Index</b></font></a>&nbsp;</td>
  <td bgcolor="#EEEEFF" class="NavBarCell1">    <a href="help-doc.html"><font class="NavBarFont1"><b>Help</b></font></a>&nbsp;</td>
  </tr>
</table>
</td>
<td bgcolor="#EEEEFF" align="right" valign="top"><em>
<b>JsWs</b></em>
</td>
</tr>

<tr>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
&nbsp;PREV&nbsp;
&nbsp;NEXT</font></td>
<td bgcolor="white" class="NavBarCell2"><font size="-2">
  <a href="index.html" target="_top"><b>FRAMES</b></a>  &nbsp;
&nbsp;<a href="overview-summary.html" target="_top"><b>NO FRAMES</b></a>
&nbsp;&nbsp;
<script>
  <!--
  if(window==top) {
    document.writeln('<A HREF="allclasses-noframe.html" TARGET=""><B>All Classes</B></A>');
  }
  //-->
</script>
<noscript>
<a href="allclasses-noframe.html" target=""><b>All Classes</b></a>
</noscript>
</font></td>
</tr>
</table>
<!-- =========== END OF NAVBAR =========== -->

<hr>
<font size="-1">

</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Thu Nov  8 22:50:16 2007</div>
</body>
</html>
